package day;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 447. 回旋镖的数量
 */
public class P447 {
    public int numberOfBoomerangs(int[][] points) {
        int[] count = new int[]{0};
        int[][] memMatrix = new int[points.length][points.length];
        bt(points, 0, 3, new ArrayList<>(), count, memMatrix);
        return count[0];
    }

    private void bt(int[][] points, int begin, int chooseCount, List<Integer> idxList, int[] count, int[][] memMatrix) {
        if (begin == points.length && chooseCount > 0) {
        } else if (chooseCount == 0) {
            //检验是否合理
            int[] p1 = points[idxList.get(0)];
            int[] p2 = points[idxList.get(1)];
            int[] p3 = points[idxList.get(2)];
            //System.out.println(String.format("{%d,%d},\t{%d,%d},\t{%d,%d}", p1[0], p1[1], p2[0], p2[1], p3[0], p3[1]));
            Collections.sort(idxList);
            int dis12 = memMatrix[idxList.get(0)][idxList.get(1)];
            int dis13 = memMatrix[idxList.get(0)][idxList.get(2)];
            int dis23 = memMatrix[idxList.get(1)][idxList.get(2)];

            if (dis12 == 0) {
                int l12_x = Math.abs(p1[0] - p2[0]);
                int l12_y = Math.abs(p1[1] - p2[1]);
                dis12 = l12_x * l12_x + l12_y * l12_y;
                memMatrix[idxList.get(0)][idxList.get(1)] = dis12;
            }
            if (dis13 == 0) {
                int l13_x = Math.abs(p1[0] - p3[0]);
                int l13_y = Math.abs(p1[1] - p3[1]);
                dis13 = l13_x * l13_x + l13_y * l13_y;
                memMatrix[idxList.get(0)][idxList.get(2)] = dis13;
            }
            if (dis23 == 0) {
                int l23_x = Math.abs(p2[0] - p3[0]);
                int l23_y = Math.abs(p2[1] - p3[1]);
                dis23 = l23_x * l23_x + l23_y * l23_y;
                memMatrix[idxList.get(1)][idxList.get(2)] = dis23;
            }
            int total = 0;
            if (dis12 == dis13) {
                total += 2;
            } else if (dis12 == dis23) {
                total += 2;
            } else if (dis13 == dis23) {
                total += 2;
            }
            count[0] += total;
        } else {
            for (int i = begin; i < points.length; i++) {
                if (!idxList.contains(i)) {
                    //选
                    idxList.add(i);
                    bt(points, i + 1, chooseCount - 1, idxList, count, memMatrix);
                    idxList.remove(idxList.size() - 1);
                }
            }
        }
    }

    public static void main(String[] args) {
        int[][] points = new int[][]{
                {0, 0}, {1, 0}, {2, 0}
        };
        int count = new P447().numberOfBoomerangs(points);
        System.out.println(count);
    }
}
